package net.pinnoocle.playservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.exception.ShopException;
import net.pinnoocle.playcommon.response.ErrorCode;
import net.pinnoocle.playcommon.utils.ConvertUtils;
import net.pinnoocle.playdao.dao.ShopWechatSettingDao;
import net.pinnoocle.playdao.entity.ShopWechatSetting;
import net.pinnoocle.playdao.entity.shopPc.WechatSettingDetailVo;
import net.pinnoocle.playdao.entity.shopPc.applet.SaveWechatSettingParams;
import net.pinnoocle.playservice.service.CommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Objects;

/**
 * 店铺小程序配置(ShopWechatSetting)表服务实现类
 *
 * @author ye
 * @since 2021-10-29 17:14:15
 */
@Service("shopWechatSettingService")
@Slf4j
public class ShopWechatSettingServiceImpl extends ServiceImpl<ShopWechatSettingDao, ShopWechatSetting> {

    @Autowired
    ShopServiceImpl shopService;
    @Autowired
    CommonService commonService;

    /**
     * 微信设置详情
     * @return
     */
    public WechatSettingDetailVo settingDetail(){
        String shopId = shopService.getShopId();
        if(StringUtils.isBlank(shopId)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"登录信息获取失败，请重新登录");
        }
        ShopWechatSetting setting = this.getOne(new QueryWrapper<ShopWechatSetting>()
                .lambda()
                .eq(ShopWechatSetting::getShopId, shopId));
        if(Objects.isNull(setting)){
            return null;
        }
        return ConvertUtils.sourceToTarget(setting,WechatSettingDetailVo.class);
    }
    /**
     * 店铺小程序设置
     * @param params
     * @return
     */
    public boolean wechatSetting(SaveWechatSettingParams params){
        String shopId = shopService.getShopId();
        if(StringUtils.isBlank(shopId)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"登录信息获取失败，请重新登录");
        }
        try {
            ShopWechatSetting setting = this.getOne(new QueryWrapper<ShopWechatSetting>()
                    .lambda()
                    .eq(ShopWechatSetting::getShopId, shopId));
            ShopWechatSetting shopWechatSetting = ConvertUtils.sourceToTarget(params, ShopWechatSetting.class);
            if(Objects.isNull(setting)){
                assert shopWechatSetting != null;
                shopWechatSetting.setShopId(shopId);
                shopWechatSetting.setCreatedTime(new Date());
                this.save(shopWechatSetting);
            }else{
                shopWechatSetting.setId(setting.getId());
                this.updateById(shopWechatSetting);
            }
        } catch (Exception e) {
            log.info("小程序设置失败："+e.getMessage());
        }
        return true;
    }

    /**
     * 上传证书
     * @param file
     * @return
     */
    public String uploadCert(MultipartFile file){
        String shopId = shopService.getShopId();
        if(StringUtils.isBlank(shopId)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"登录信息获取失败，请重新登录");
        }
        return commonService.uploadCertFile(file, shopId);
    }
}